package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.bo.PageInfo;
import com.yixin.hospital.entity.domain.Drug;
import com.yixin.hospital.entity.dto.req.drug.DrugAddReqDTO;
import com.yixin.hospital.entity.dto.req.drug.DrugEditReqDTO;
import com.yixin.hospital.entity.dto.req.drug.DrugPicEditReqDTO;
import com.yixin.hospital.entity.dto.req.visit.VisitSubPackageDrugReqDTO;
import com.yixin.hospital.entity.vo.DrugDepositoryAllDrugVo;
import com.yixin.hospital.entity.vo.DrugDepositoryDrugVo;
import com.yixin.hospital.entity.vo.DrugVo;
import com.yixin.hospital.entity.vo.PurchaseOrderVO;
import com.yixin.hospital.service.DrugService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/drug")
@Api(value = "药品管理", tags = "药品管理")
@Slf4j
public class DrugController {

    @Resource
    private DrugService drugService;

    @GetMapping("/list")
    @ApiOperation(value = "药品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstType", value = "一级分类", paramType = "query"),
            @ApiImplicitParam(name = "secondType", value = "二级分类", paramType = "query"),
            @ApiImplicitParam(name = "thirdType", value = "三级分类", paramType = "query"),
    })
    public ApiResult<PageInfo<DrugVo>> list(@RequestParam int currentPage, @RequestParam int pageSize,
                             @RequestParam(required = false) Long firstType, @RequestParam(required = false) Long secondType,
                             @RequestParam(required = false) Long thirdType, @RequestParam(required = false) String pinyinCode) {
        return drugService.allList(currentPage, pageSize, firstType, secondType, thirdType, pinyinCode);
    }

    @GetMapping("/get-drug-code")
    @ApiOperation(value = "获取药品code")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstType", value = "一级分类", paramType = "query"),
            @ApiImplicitParam(name = "secondType", value = "二级分类", paramType = "query"),
            @ApiImplicitParam(name = "thirdType", value = "三级分类", paramType = "query"),
    })
    public ApiResult<String> getDrugCode(@RequestParam() int firstType, @RequestParam() int secondType,
                                 @RequestParam() int thirdType) {
        return drugService.getDrugCode(firstType, secondType, thirdType);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加药品")
    @Transactional
    public ApiResult<?> add(@Valid @RequestBody DrugAddReqDTO drugAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugService.add(drugAddReqDTO, userName);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑药品")
    @Transactional
    public ApiResult<?> edit(@Valid @RequestBody DrugEditReqDTO drugEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugService.edit(drugEditReqDTO, userName);
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除药品")
    public ApiResult remove(@PathVariable int id) {
        drugService.removeById(id);
        return ApiResult.success();
    }

    @PutMapping("/edit-drug-pic")
    @ApiOperation(value = "编辑药品图片")
    public ApiResult editDrugPic(@Valid @RequestBody DrugPicEditReqDTO drugPicEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugService.editDrugPic(drugPicEditReqDTO, userName);
    }

    @GetMapping("/query-pic-by-id/{id}")
    @ApiOperation(value = "根据code搜索药品图片")
    public ApiResult queryPicById(@PathVariable Long id) {
        return drugService.queryPicById(id);
    }

    @GetMapping("/query-by-pinyin-code/{pinyinCode}")
    @ApiOperation(value = "根据拼音简码搜索药品")
    public ApiResult<List<DrugVo>> queryByPinyinCode(@PathVariable String pinyinCode) {
        return drugService.queryByPinyinCode(pinyinCode);
    }

    @GetMapping("/query-by-pinyin-code/{pinyinCode}/{optType}")
    @ApiOperation(value = "根据拼音简码搜索药品")
    public ApiResult<List<DrugVo>> queryByPinyinCodeAndOptType(@PathVariable String pinyinCode, @PathVariable Integer optType, HttpServletRequest request) {
        return drugService.queryByCodeAndOptType(pinyinCode, optType, request);
    }

    @GetMapping("/query-by-pinyin-code-productTime/{pinyinCode}/{depositoryIds}/{type}")
    @ApiOperation(value = "根据拼音简码搜索药品")
    public ApiResult<List<DrugVo>> queryByPinyinCodeProductTime(@PathVariable String pinyinCode, @PathVariable List<Long> depositoryIds, @PathVariable Integer type) {
        return drugService.queryByPinyinCodeAndDepository(pinyinCode, depositoryIds, type);
    }

    @GetMapping("/query-by-code-depositoryId")
    @ApiOperation(value = "根据药品Id 库房id 查库存")
    public ApiResult<DrugDepositoryDrugVo> queryByCodeAnDepositoryId(@RequestParam("drugCode") String drugCode,
                                               @RequestParam("depositoryId") Long depositoryId) {
        return drugService.queryByCodeDepositoryId(drugCode, depositoryId);
    }

    @GetMapping("/depository-num-list")
    @ApiOperation(value = "根据药品Id 库房id 查库存列表")
    public ApiResult<IPage<DrugDepositoryDrugVo>> queryDepositoryNumList(@RequestParam int currentPage, @RequestParam int pageSize,
                                               @RequestParam(value = "drugName", required = false) String drugName,
                                               @RequestParam(value = "drugDepositoryId") Long drugDepositoryId, HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return drugService.depositoryNumList(currentPage, pageSize, drugName, drugDepositoryId, userId);
    }

    @GetMapping("/depository-num-detail")
    @ApiOperation(value = "根据药品Id 库房id 查库存详情")
    public ApiResult<List<DrugDepositoryDrugVo>> queryDepositoryNumDetail(@RequestParam(value = "drugCode", required = false) String drugCode,
                                              @RequestParam(value = "drugDepositoryId", required = false) Long drugDepositoryId) {
        return drugService.depositoryNumDetail(drugCode, drugDepositoryId);
    }

    @GetMapping("/drug-detail-by-codes")
    @ApiOperation(value = "根据药品code 查详情")
    public ApiResult queryDrugByCodes(@RequestParam(value = "drugCodes", required = false) String drugCodes) {
        return ApiResult.success(drugService.list(new LambdaQueryWrapper<Drug>().in(Drug::getCode, Arrays.asList(drugCodes.split(",")))));
    }

    @GetMapping("/all-drug-depository-num")
    @ApiOperation(value = "所有药品列表与库存, 开始集采")
    public ApiResult<PurchaseOrderVO> queryDepositoryAllNum(HttpServletRequest request) {
        return drugService.queryDepositoryAllNum(request);
    }

    @GetMapping("/drug-depository-num")
    @ApiOperation(value = "单个药品与各库库存, 开始集采")
    public ApiResult<DrugDepositoryAllDrugVo> queryDepositoryNum(String drugCode, HttpServletRequest request) {
        return drugService.queryDepositoryNum(drugCode, request);
    }


    @PostMapping("/take-drug")
    @ApiOperation(value = "取药（分装 整装）")
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<?> takeDrug(@Valid @RequestBody VisitSubPackageDrugReqDTO visitSubPackageDrugReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        return drugService.takeDrug(visitSubPackageDrugReqDTO, userName);
    }

}
